<?php

namespace App\Web\Controllers;

use App\Models\Category;
use App\Services\CategoryService;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\WebController;

class HomeController extends WebController
{
    protected $categoryService;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(CategoryService $categoryService)
    {
//        $this->middleware('auth');
        $this->categoryService = $categoryService;
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('web/home')
            ->with('categories', $this->categoryService->getCategoriesByPid())
            ->with('cid', request()->get('cid'));
    }

    /**
     * 验证邮箱
     * @param $token
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function verifyEmail($token){

        $user = app('App\Services\EmailService')->verifyEmail($token);

        if (!$user){
            myflash()->error('邮箱验证失败！');
            return redirect('/');
        }else{
            \Auth::login($user);
            myflash()->success('邮箱验证成功！');

            return redirect('/');
        }
    }

    /**
     * 获取分类通过pid
     * @return mixed
     */
    public function getCategoryByPid(){
        if (!request()->has('cid')) return message(false, '参数错误');

        return message(true, 'success', $this->categoryService->getCategoriesByPid(\request()->get('cid')));
    }
}
